package com.immortal.ai.config;

import com.immortal.ai.tools.TestTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.immortal.ai.constants.SystemConstants.*;

/**
 * @author ywh
 * @version 1.0
 * @description: TODO
 * @date 2025/6/9 17:44
 */
@Configuration
public class CommonConfiguration {



    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    /**
     * @description: 配置对话模型客户端
     * @author: ywh
     * @date: 2025/6/19 15:34
     */
    @Bean
    public ChatClient chatClient(OpenAiChatModel model) {
        return ChatClient.builder(model)
                .build();
    }


    /**
     * @description: prompt
     * @author: ywh
     * @date: 2025/6/16 15:52
     */
    @Bean(name = "fortuneTellingChatClient")
    public ChatClient fortuneTellingChatClient(OpenAiChatModel model) {
        return ChatClient.builder(model)
                .defaultSystem(FORTUNE_TELLING_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(), //配置日志advisor
                        new MessageChatMemoryAdvisor(chatMemory())
                )
                .build();
    }

    /**
     * @description: function calling
     * @author: ywh
     * @date: 2025/6/16 15:52
     */
    @Bean(name = "functionCallChatClient")
    public ChatClient functionCallChatClient(OpenAiChatModel model,TestTools tools) {
        return ChatClient.builder(model)
                .defaultSystem(FUNCTION_CALLING_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory())
                )
                .defaultTools(tools)
                .build();
    }


    /**
     * @description: 构建本地向量库
     * @author: ywh
     * @date: 2025/6/17 14:56
     */
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }


    /**
     * @description: RAGChatClient
     * @author: ywh
     * @date: 2025/6/18 9:59
     */
    @Bean
    public ChatClient pdfChatClient (OpenAiChatModel model,VectorStore vectorStore){
        return ChatClient.builder(model)
                .defaultSystem("请根据上下文回答问题，遇到上下文没有的问题，不要随意编造。")
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory()),
                        new SimpleLoggerAdvisor(),
                        new QuestionAnswerAdvisor(
                                vectorStore, //向量库
                                SearchRequest.builder()
                                        .similarityThreshold(0.6d) //相似度阈值
                                        .topK(2) //返回文档片段数量
                                        .build()
                        )

                )
                .build();
    }



}
